package com.app;

import android.content.Context;

import com.iflytek.autoupdate.IFlytekUpdate;
import com.iflytek.autoupdate.IFlytekUpdateListener;
import com.iflytek.autoupdate.UpdateConstants;

import java.io.File;

public class IFlytek {


	public static void onCreate_init(Context context, IFlytekUpdateListener updateListener) {

		IFlytekUpdate updManager;
		updManager = IFlytekUpdate.getInstance(context);
		updManager.setDebugMode(G.DEBUG);
		updManager.setParameter(UpdateConstants.EXTRA_WIFIONLY, "true");


		updManager.setParameter(UpdateConstants.EXTRA_STYLE, UpdateConstants.UPDATE_UI_DIALOG);
		updManager.autoUpdate(context, updateListener);
	}


	/*
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.diaglog:
			updManager.setParameter(UpdateConstants.EXTRA_STYLE, UpdateConstants.UPDATE_UI_DIALOG);
			updManager.autoUpdate(IFlytek.this, updateListener);
			break;
		case R.id.noti:
			updManager.setParameter(UpdateConstants.EXTRA_STYLE, UpdateConstants.UPDATE_UI_NITIFICATION);
			updManager.autoUpdate(IFlytek.this, updateListener);
			break;
		case R.id.btn_delete:
			// 删除已下载的更新文件
			String path = "";
			if(Environment.MEDIA_MOUNTED.equalsIgnoreCase(Environment.getExternalStorageState())) {
				path = Environment.getExternalStorageDirectory() + "/download/AutoUpdate"; 
			}
			if(TextUtils.isEmpty(path)) {
				showTip("文件路径不正确！");
				return;
			}
			File file = new File(path);
			delFile(file);
			showTip("文件已删除！");
		default:
			break;
		}
	}
	*/

	/*
	private IFlytekUpdateListener updateListener = new IFlytekUpdateListener() {
		
		@Override
		public void onResult(int errorcode, UpdateInfo result) {
			
			if(errorcode == UpdateErrorCode.OK && result!= null) {
				if(result.getUpdateType() == UpdateType.NoNeed) {
					showTip("已经是最新版本！");
					return;
				}
				updManager.showUpdateInfo(IFlytek.this, result);
			}
			else
			{
				showTip("请求更新失败！\n更新错误码：" + errorcode);
			}
		}
	};
	*/
	
	private void delFile(File deleteFile) {
		if(!deleteFile.exists()) {
			return;
		}
		if(!deleteFile.isDirectory()) {
			deleteFile.delete();
		} else {
			File[] fileList = deleteFile.listFiles();
			if(null == fileList || fileList.length<=0) {
				deleteFile.delete();
				return;
			}
			for(File file : fileList) {
				delFile(file);
			}
			deleteFile.delete();
		}
	}
	
	private void showTip(final String str) {
		/*
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mToast.setText(str);
				mToast.show();
			}
		});
		*/
	}
}
